package com.example.cores_sqlite;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;

import com.example.cores_sqlite.entity.User;
import com.example.cores_sqlite.utils.ToastHelper;
import com.example.cores_sqlite.utils.UserDBHelper;

import java.util.List;

public class SqliteCrud extends AppCompatActivity implements View.OnClickListener {
    TextView etName;
    TextView etAge;
    TextView etHeight;
    TextView etWeight;
    CheckBox cbMarried;
    private UserDBHelper helper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_sqlite_crud);

        etName = findViewById(R.id.et_name);
        etAge = findViewById(R.id.et_age);
        etHeight = findViewById(R.id.et_height);
        etWeight = findViewById(R.id.et_weight);
        cbMarried = findViewById(R.id.cb_married);

        findViewById(R.id.btn_c).setOnClickListener(this);
        findViewById(R.id.btn_r).setOnClickListener(this);
        findViewById(R.id.btn_u).setOnClickListener(this);
        findViewById(R.id.btn_d).setOnClickListener(this);

//        onStart();
    }

    @Override
    protected void onStart() {
        super.onStart();
        // 获取实例，打开读写链接
        helper = UserDBHelper.getInstance(this);
        helper.openReadLink();
        helper.openWriteLink();
    }

    @Override
    protected void onStop() {
        super.onStop();
        helper.close();
    }

    private User getUserFromList() {
        String name = etName.getText().toString();
        String age = etAge.getText().toString();
        String weight = etWeight.getText().toString();
        String height = etHeight.getText().toString();
        boolean married = cbMarried.isChecked();

        Log.d("zhoutao", "name:" + name);
        Log.d("zhoutao", "age:" + age);
        Log.d("zhoutao", "weight:" + weight);
        Log.d("zhoutao", "height:" + height);
        Log.d("zhoutao", "married:" + married);

        User user = new User(
                name,
                Integer.valueOf(age),
                Long.valueOf(height),
                Float.valueOf(weight),
                married
        );
        return user;
    }

    private void createUser() {
        User user = getUserFromList();
        boolean res = helper.insert(user) > 0;
        showResult(res, "创建");
    }

    private void showResult(boolean isSuccess, String tip) {
        if (isSuccess) {
            ToastHelper.show(this, tip + "成功");
        } else {
            ToastHelper.show(this, tip + "失败");
        }
    }

    private void removeUserByName() {
        String name = etName.getText().toString();
        boolean deleteSuccess = helper.delete(name) > 0;
        showResult(deleteSuccess, "删除");
    }

    private void updateUserByName() {
        Log.d("zhoutao", "updateUserByName: ");
        User user = getUserFromList();
        boolean result = helper.update(user) > 0;
        showResult(result, "更新");
    }

    private void findAll() {
        List<User> list = helper.select();
        for (User user : list) {
            Log.d("zhoutao", user.toString());
        }
    }

    private void findByName() {
        String name = etName.getText().toString();
        List<User> list = helper.selectByName(name);
        for (User user : list) {
            Log.d("zhoutao", user.toString());
        }
    }

    public void onClick(View view) {

        Log.d("zhoutao", "onClick: ");
        int id = view.getId();
        // crate
        if (id == R.id.btn_c) {
            createUser();
        } else if (id == R.id.btn_d) {
            removeUserByName();
        } else if (id == R.id.btn_u) {
            updateUserByName();
        } else if (id == R.id.btn_r) {
            // findAll();
            findByName();
        }
    }


}